var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });




    layer.ready(function () {

        //获取产品目录信息
        $.ajax({
            type : 'get',
            url:'productcatalog/getProductDirectoryInfo',
            dataType:"json",
            async:false,
            success : function (data) {
                if(data.status==200 ){
                    for (let i in data.data){
                        $("#ProductCatalog").append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            },
        });

    });
    form.render();//表单刷新

    let initData = [];
    table.render({
        elem: '#statisticsSelect',
        data: initData,
        url: 'productmaininfo/statistics/productDetailStatisticsListByPage',
        toolbar: '#statisticsToolbar',
        cols: [[
            {field: 'numbers',title:'序号',type:'numbers'},
            {field: 'productCode', title: '产品编码', sort: true,width: 120, event: "productCode", align: 'center'},
            {field: 'specificationCode', title: '规格编码',width: 300, align: 'center'},
            {field:'specificationExplanation', title:'规格说明', width: 200, align: 'center'},
            {field:'ge', title:'创建日期', sort : true,width: 120, align: 'center'},
            {field:'chineseDesc',title: '中文描述', width: 300, align: 'center'},
            {field:'englishDesc',title: '英文描述', width: 300, align: 'center'},
            {field: 'taxIncludedPrice', title:'含税出厂价',sort:true,width: 120, align: 'center'},
            {field: 'untaxedPrice', title: '未税出厂价', sort: true,width: 120, align: 'center'},
            {field: 'taxIncludedFob', title:'含税FOB价',sort:true,width: 120, align: 'center'},
            {field: 'untaxedFob', title: '未税FOB价', sort: true,width: 120, align: 'center'},
            {field: 'grossWeight', title: '毛重', sort: true,width: 120, align: 'center'},
            {field: 'netWeight', title: '净重', sort: true,width: 120, align: 'center'},
            {field: 'contents', title: '所属目录',width: 180, align: 'center'},
            {field: 'contentsParent', title: '所属目录上级',width: 180, align: 'center'},
            {field: 'id', hide:true}

        ]]
        , id: 'statisticsTableId'
        , height: $(document).height() - $('#statisticsSelect').offset().top - 50
        , page: true
        , limit: 300
        , limits: [300, 600, 900]
    });

    table.on('tool(statisticsTable)', function(obj){
        let id = obj.data.id;
        if (obj.event === "productCode"){
            parent.intab("views/jsp/BasicData/tpl/viewproduct.jsp?id="+id,"产品详情")
        }
    });

    form.on('submit(submitform)', function (data) {

        let cataLog=data.field;
        let code=cataLog.ProductCatalogCode;
        if (code !="" || code !=null || code !=NaN){
            $.ajax({
                type : 'get',
                data:{"code":code},
                url:'productmaininfo/getCatalogBycode',
                dataType:"json",
                async:false,
                success : function (da) {
                    if(da.status==200 ){
                        cataLog.ProductCatalogCode=da.data.join(',');
                    }
                },

            });
        }



        table.reload('statisticsTableId', {
            page: {
                limit:300,
                curr: 1
            }
            , where: cataLog
        }, 'data');
        return false;
    });

    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"productmaininfo/productDetailStatisticsExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'X': 120
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '产品创建日期及修改日期统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

});